package com.cpe.transfer.physique;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.cpe.data.domain.lesson.Lesson;
import com.cpe.data.domain.physique.desc.PhysiqueDesc;
import com.cpe.data.domain.physique.desc.PhysiqueDescInfo;
import com.cpe.data.domain.physique.desc.PhysiqueDescInfo.RecommendLesson;
import com.cpe.env.LandingEnv;
import com.cpe.repository.mapper.lesson.LessonMapper;
import com.google.common.base.Splitter;

import jodd.util.StringUtil;

@Component
public class PhysiqueDescTransfer {

	@Resource
	private LessonMapper lessonMapper;

	@Resource
	private LandingEnv landingEnv;

	/**
	 * 实体-->响应
	 */
	public PhysiqueDescInfo toInfo(PhysiqueDesc physiqueDesc) {
		Assert.notNull(physiqueDesc);
		PhysiqueDescInfo info = new PhysiqueDescInfo();
		BeanUtils.copyProperties(physiqueDesc, info);
		// 推荐URL
		String urls = physiqueDesc.getRecommendUrls();
		if (StringUtil.isNotBlank(urls)) {
			List<String> urlList = Splitter.on(";").splitToList(urls);
			for (int i = 0; i < urlList.size(); i++) {
				String url = urlList.get(i);
				Lesson lesson = lessonMapper.findById(url);
				if (lesson == null) {
					continue;
				}
				url = landingEnv.getLessonDetail() + url;
				RecommendLesson recommendLesson = new RecommendLesson();
				recommendLesson.setUrl(url);
				recommendLesson.setTitle(lesson.getTitle());
				info.getRecommendLessons().add(recommendLesson);
			}
		}
		return info;
	}
}
